// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Logo Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0  : object.timer
private alias object.value1  : object.playerPos.x
private alias object.value2  : object.playerPos.y
private alias object.value3  : object.paletteTimer
private alias object.value4  : object.arrayPos
private alias object.value5  : object.armFrame
private alias object.value6  : object.armPos.x
private alias object.value7  : object.armPos.y
private alias object.value8  : object.charTitlePos
private alias object.value9  : object.speedPercent
private alias object.value10 : object.disablePaletteRotation // Not actually used as far as I can tell
private alias object.value11 : object.player2Pos.x
private alias object.value12 : object.player2Pos.y
private alias object.value13 : object.p2Frame
private alias object.value14 : object.levelSelectCheatPos
private alias object.value15 : object.debugModeCheatPos
private alias object.value16 : object.regionSwitchCheatPos
private alias object.value17 : object.character

// Alias for the character workaround
private alias temp1 : temp.titleCharacter

// States
// The first 3 states are identical so there's really not much reason to have em seperate but it is what it is

// Sonic States
private alias 0  : LOGO_SETUP_SONIC
private alias 1  : LOGO_FADEIN_SONIC
private alias 2  : LOGO_RISEDELAY_SONIC
private alias 3  : LOGO_RISE_SONIC
private alias 4  : LOGO_ANIMATE_SONIC
private alias 5  : LOGO_FINGERWIGGLE_SONIC
private alias 6  : LOGO_P2TEXTAPPEAR_SONIC
private alias 7  : LOGO_MOVELEFT_SONIC
private alias 8  : LOGO_RISEP2_SONIC
private alias 9  : LOGO_ANIMATEP2_SONIC
private alias 10 : LOGO_ARMAPPEARP2_SONIC
private alias 11 : LOGO_ANIMATEP1_SONIC

// Tails States
private alias 0  : LOGO_SETUP_TAILS
private alias 1  : LOGO_FADEIN_TAILS
private alias 2  : LOGO_RISEDELAY_TAILS
private alias 3  : LOGO_RISE_TAILS
private alias 4  : LOGO_ANIMATE_TAILS
private alias 5  : LOGO_ARMAPPEAR_TAILS
private alias 6  : LOGO_PREPLOGOAPPEAR_TAILS
private alias 7  : LOGO_SHOWLOGO_TAILS
private alias 8  : LOGO_IDLE_TAILS

// Knux States
private alias 0  : LOGO_SETUP_KNUX
private alias 1  : LOGO_FADEIN_KNUX
private alias 2  : LOGO_RISEDELAY_KNUX
private alias 3  : LOGO_RISE_KNUX
private alias 4  : LOGO_ANIMATE_KNUX
private alias 5  : LOGO_ARMAPPEAR_KNUX
private alias 6  : LOGO_ANIMATEARM_KNUX
private alias 7  : LOGO_MOVEARM_KNUX
private alias 8  : LOGO_PREPLOGOAPPEAR_KNUX
private alias 9  : LOGO_ARMAPPEAR2_KNUX
private alias 10 : LOGO_ANIMATEARM2_KNUX
private alias 11 : LOGO_MOVEARM2_KNUX
private alias 12 : LOGO_SHOWLOGO_KNUX
private alias 13 : LOGO_IDLE_KNUX

// Game Modes
private alias 0 : MODE_NOSAVE

// Player List Pos Aliases
private alias 3 : PLAYER_SONIC_AND_TAILS


// ========================
// Function Declarations
// ========================

reserve function Logo_CheckButtonCheatCodes


// ========================
// Tables
// ========================

private table Logo_playerPalette
	0x6060A0, 0x80A0E0, 0xE08000, 0xE0C080, 0xA04000, 0x600000, 0xE02080, 0xE00040, 0x800000, 0x806080, 0xA08060, 0xE0C0A0, 0x80A0A0, 0xA04020
end table

private table Logo_tailsBodyOffsets
	-50, -40, -53, -56, -58, -68, -60, -74, -62, -78, -63, -79, -64, -80
end table

private table Logo_tailsArmOffsets
	5, -32, 6, -31
end table


// ========================
// Function Definitions
// ========================

private function Logo_CheckButtonCheatCodes
	temp0 = 0 // keyID
	
	if keyPress[0].up == true
		temp0 = 1
	end if

	if keyPress[0].down == true
		temp0 = 2
	end if

	if keyPress[0].left == true
		temp0 = 3
	end if

	if keyPress[0].right == true
		temp0 = 4
	end if

	if keyPress[0].buttonA == true
		temp0 = 5
	end if

	if keyPress[0].buttonB == true
		temp0 = 6
	end if

	if keyPress[0].buttonC == true
		temp0 = 7
	end if
	
	// Level Select Cheat
	// 1, 2, 3, 4
	// up, down, left, right
	switch object.levelSelectCheatPos
	case 0
		if temp0 == 1
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 1
		if temp0 == 2
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 2
		if temp0 == 3
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break
		
	case 3
		if temp0 == 4
			object.levelSelectCheatPos++
			object.levelSelectCheatPos = 0
			options.stageSelectFlag = true
			PlaySfx(SfxName[Ring L], false)
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break
		
	end switch
	
	// Debug Mode Cheat
	// 1, 7, 2, 7, 3, 7, 4, 7
	// up, C, down, C, left, C, right, C
	switch object.debugModeCheatPos
	case 0
		if temp0 == 1
			object.debugModeCheatPos++
		else
			if temp0 != 0
				object.debugModeCheatPos = 0
			end if
		end if
		break

	case 1
		if temp0 == 7
			object.debugModeCheatPos++
		else
			if temp0 != 0
				object.debugModeCheatPos = 0
			end if
		end if
		break

	case 2
		if temp0 == 2
			object.debugModeCheatPos++
		else
			if temp0 != 0
				object.debugModeCheatPos = 0
			end if
		end if
		break

	case 3
		if temp0 == 7
			object.debugModeCheatPos++
		else
			if temp0 != 0
				object.debugModeCheatPos = 0
			end if
		end if
		break

	case 4
		if temp0 == 3
			object.debugModeCheatPos++
		else
			if temp0 != 0
				object.debugModeCheatPos = 0
			end if
		end if
		break

	case 5
		if temp0 == 7
			object.debugModeCheatPos++
		else
			if temp0 != 0
				object.debugModeCheatPos = 0
			end if
		end if
		break

	case 6
		if temp0 == 4
			object.debugModeCheatPos++
		else
			if temp0 != 0
				object.debugModeCheatPos = 0
			end if
		end if
		break

	case 7
		if temp0 == 7
			object.debugModeCheatPos = 0
			stage.debugMode = true
			PlaySfx(SfxName[Ring L], false)
		else
			if temp0 != 0
				object.debugModeCheatPos = 0
			end if
		end if
		break
		
	end switch
	
	// Region Switch Cheat
	// 1, 2, 2, 2, 2, 1
	// up, down, down, down, down, up
	switch object.regionSwitchCheatPos
	case 0
		if temp0 == 1
			object.regionSwitchCheatPos++
		else
			if temp0 != 0
				object.regionSwitchCheatPos = 0
			end if
		end if
		break

	case 1
	case 2
	case 3
	case 4
		if temp0 == 2
			object.regionSwitchCheatPos++
		else
			if temp0 != 0
				object.regionSwitchCheatPos = 0
			end if
		end if
		break

	case 5
		if temp0 == 1
			object.regionSwitchCheatPos++
			object.regionSwitchCheatPos = 0
			options.region ^= 1
			PlaySfx(SfxName[Ring L], false)
		else
			if temp0 != 0
				object.regionSwitchCheatPos = 0
			end if
		end if
		break
		
	end switch
end function


// ========================
// Events
// ========================

event ObjectUpdate

Abs(tileLayer[1].parallaxFactor)
FlipSign(tileLayer[1].parallaxFactor)
tileLayer[1].scrollPos = 0x110000
tileLayer[2].scrollPos = 0x110000

#platform: USE_ORIGINS
	if object.state <= LOGO_FADEIN_SONIC
		object.character = stage.playerListPos
	end if
#endplatform

#platform: USE_STANDALONE
	if stage.player2Enabled == true
		stage.playerListPos = PLAYER_SONIC_AND_TAILS
	end if
#endplatform

	if object.disablePaletteRotation == false
		object.paletteTimer++
		if object.paletteTimer == 6
			object.paletteTimer = 0
			RotatePalette(0, 192, 195, true)
		end if
	end if

#platform: USE_STANDALONE
	// Check for cheat codes in No Save Mode
	if options.gameMode == MODE_NOSAVE
#endplatform
		if object.state > LOGO_RISE_SONIC
			CallFunction(Logo_CheckButtonCheatCodes)
		end if
#platform: USE_STANDALONE
	end if
#endplatform
end event


event ObjectDraw
	temp0 = true

	// Have to use a temp value workaround due to compiler shenanigans
	// Normally, it's just `switch stage.playerListPos` or `switch object.character`, but we can't wrap a switch start with platform checks so this is the next best thing
#platform: USE_STANDALONE
	temp.titleCharacter = stage.playerListPos
#endplatform
#platform: USE_ORIGINS
	temp.titleCharacter = object.character
#endplatform

	switch temp.titleCharacter
	case 1
		if object.state == LOGO_P2TEXTAPPEAR_SONIC
			temp0 = false
		end if
		break

	case 2
		if object.state == LOGO_IDLE_TAILS
			temp0 = false
		end if
		break

	case 3
		if object.state == LOGO_ANIMATEARM_KNUX
			temp0 = false
		end if
		break

	end switch

	// Draw Logo (Top Half)
	if temp0 != false
		DrawSpriteScreenXY(9, screen.xcenter, object.ypos)
	end if

	switch temp.titleCharacter
	case 0
	case 3
		switch object.state
		case LOGO_SETUP_SONIC
			screen.xoffset 		 = 0

			object.priority 	 = PRIORITY_ACTIVE
			object.timer		 = 0x100
			stage.activeLayer[0] = 1 //set bg to lowest layer
			object.state++

			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
			break

		case LOGO_FADEIN_SONIC
			screen.xoffset += 1

			if object.timer > 0
				object.timer -= 8
			else
				PlayMusic(0)
				object.state++
			end if

			SetScreenFade(0x00, 0x00, 0x00, object.timer)
			break

		case LOGO_RISEDELAY_SONIC
			screen.xoffset += 1

			object.timer++
			if object.timer == 38
				object.timer 		= 0
				object.playerPos.y 	= 186
				object.frame 		= 0
				object.state++
			end if
			break

		case LOGO_RISE_SONIC
			screen.xoffset += 1

			object.playerPos.y -= 8
			if object.playerPos.y < 144
				object.frame = 1
			end if

			if object.playerPos.y <= 112
				object.playerPos.y = 112
				object.state++
			end if

			DrawSpriteScreenXY(object.frame, screen.xcenter, object.playerPos.y)
			break

		case LOGO_ANIMATE_SONIC
			screen.xoffset += 1

			object.timer++
			if object.timer == 8
				object.frame++
				if object.frame >= 7
					object.frame = 7
					object[+1].type 	= TypeName[Start Message]
					object[+1].priority = PRIORITY_ACTIVE
					object[+1].xpos 	= screen.xcenter
					
					if temp.titleCharacter == 3
						object[+1].ypos = 216
						object.state 	= LOGO_P2TEXTAPPEAR_SONIC
					else
						object[+1].ypos = 192
						object.state++
					end if
				end if
				object.timer = 0
			end if

			DrawSpriteScreenXY(object.frame, screen.xcenter, object.playerPos.y)
			break

		case LOGO_FINGERWIGGLE_SONIC
			screen.xoffset += 1

			object.timer++
			if object.timer == 8
				object.frame++
				if object.frame > 8
					object.frame = 7
				end if
				object.timer = 0
			end if

			DrawSpriteScreenXY(9, screen.xcenter, object.ypos)
			DrawSpriteScreenXY(object.frame, screen.xcenter, object.playerPos.y)
			break

		case LOGO_P2TEXTAPPEAR_SONIC
			screen.xoffset += 1

			object.ypos -= 2
			object.playerPos.y -= 2
			if object.ypos <= -24
				object.charTitlePos = screen.xsize
				object.charTitlePos += 108
				object.state++
				object.playerPos.x = screen.xcenter
			end if

			DrawSpriteScreenXY(9, screen.xcenter, object.ypos)
			DrawSpriteScreenXY(object.frame, screen.xcenter, object.playerPos.y)
			break

		case LOGO_MOVELEFT_SONIC
			screen.xoffset += 1

			object.timer++
			object.playerPos.x++
			object.charTitlePos -= 8
			temp0 = screen.xcenter
			temp0 += 12
			if object.charTitlePos <= temp0
				object.charTitlePos = temp0
				object.p2Frame = 12
				object.state++
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)

			if options.region == 1
				DrawSpriteScreenXY(33, object.charTitlePos, 228)
			else
				DrawSpriteScreenXY(31, object.charTitlePos, 228)
			end if

			DrawSpriteScreenXY(36, object.charTitlePos, 228)
			break

		case LOGO_RISEP2_SONIC
			screen.xoffset += 1

			temp0 = object.arrayPos
			GetTableValue(object.player2Pos.x, temp0, Logo_tailsBodyOffsets)
			object.player2Pos.x += screen.xcenter
			object.player2Pos.x -= 24

			temp0++
			GetTableValue(object.player2Pos.y, temp0, Logo_tailsBodyOffsets)
			object.player2Pos.y += screen.ycenter
			object.player2Pos.y -= 24

			object.timer++
			object.timer &= 3
			if object.timer == 0
				object.arrayPos += 2
				if object.arrayPos == 14
					object.state++
				end if
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			DrawSpriteScreenXY(object.p2Frame, object.player2Pos.x, object.player2Pos.y)

			if options.region == 1
				DrawSpriteScreenXY(33, object.charTitlePos, 228)
			else
				DrawSpriteScreenXY(31, object.charTitlePos, 228)
			end if

			DrawSpriteScreenXY(36, object.charTitlePos, 228)
			break

		case LOGO_ANIMATEP2_SONIC
			screen.xoffset += 1

			object.timer++
			if object.timer == 2
				object.p2Frame++
				if object.p2Frame >= 16
					object.p2Frame = 16
					object.armPos.x = 8
					object.armPos.x += screen.xcenter
					object.armPos.y = -27
					object.armPos.y += screen.ycenter
					object.arrayPos = 0
					object.state++
				end if
				object.timer = 0
			end if

			DrawSpriteScreenXY(object.p2Frame, object.player2Pos.x, object.player2Pos.y)

			if object.p2Frame == 16
				DrawSpriteScreenXY(17, object.armPos.x, object.armPos.y)
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)

			if options.region == 1
				DrawSpriteScreenXY(33, object.charTitlePos, 228)
			else
				DrawSpriteScreenXY(31, object.charTitlePos, 228)
			end if

			DrawSpriteScreenXY(36, object.charTitlePos, 228)
			break

		case LOGO_ARMAPPEARP2_SONIC
			screen.xoffset += 1

			temp0 = object.arrayPos
			GetTableValue(object.armPos.x, temp0, Logo_tailsArmOffsets)
			object.armPos.x += screen.xcenter
			object.armPos.x -= 24

			temp0++
			GetTableValue(object.armPos.y, temp0, Logo_tailsArmOffsets)
			object.armPos.y += screen.ycenter
			object.armPos.y -= 24

			object.timer++
			object.timer &= 3
			if object.timer == 0
				object.arrayPos += 2
				if object.arrayPos == 4
					object.state++
				end if
			end if

			DrawSpriteScreenXY(object.p2Frame, object.player2Pos.x, object.player2Pos.y)
			DrawSpriteScreenXY(17, object.armPos.x, object.armPos.y)
			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)

			if options.region == 1
				DrawSpriteScreenXY(33, object.charTitlePos, 228)
			else
				DrawSpriteScreenXY(31, object.charTitlePos, 228)
			end if

			DrawSpriteScreenXY(36, object.charTitlePos, 228)
			break

		case LOGO_ANIMATEP1_SONIC
			screen.xoffset += 1

			object.timer++
			if object.timer == 8
				object.frame++
				if object.frame > 8
					object.frame = 7
				end if
				object.timer = 0
			end if

			DrawSpriteScreenXY(object.p2Frame, object.player2Pos.x, object.player2Pos.y)
			DrawSpriteScreenXY(17, object.armPos.x, object.armPos.y)
			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)

			if options.region == 1
				DrawSpriteScreenXY(33, object.charTitlePos, 228)
			else
				DrawSpriteScreenXY(31, object.charTitlePos, 228)
			end if
			
			DrawSpriteScreenXY(36, object.charTitlePos, 228)
			break
			
		end switch
		break

	case 1
		switch object.state
		case LOGO_SETUP_TAILS
			screen.xoffset 		 = 0

			object.priority 	 = PRIORITY_ACTIVE
			object.timer		 = 0x100
			stage.activeLayer[0] = 1
			object.state++

			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
			break

		case LOGO_FADEIN_TAILS
			screen.xoffset += 2

			if object.timer > 0
				object.timer -= 8
			else
				PlayMusic(0)
				object.state++
			end if

			SetScreenFade(0x00, 0x00, 0x00, object.timer)
			break

		case LOGO_RISEDELAY_TAILS
			screen.xoffset += 2

			object.timer++
			if object.timer == 38
				object.timer = 0
				object.frame = 12
				object.state++
			end if
			break

		case LOGO_RISE_TAILS
			screen.xoffset += 2

			temp0 = object.arrayPos
			GetTableValue(object.playerPos.x, temp0, Logo_tailsBodyOffsets)
			object.playerPos.x += screen.xcenter
			temp0++

			GetTableValue(object.playerPos.y, temp0, Logo_tailsBodyOffsets)
			object.playerPos.y += screen.ycenter

			object.timer++
			object.timer &= 3
			if object.timer == 0
				object.arrayPos += 2
				if object.arrayPos == 14
					object.state++
				end if
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			break

		case LOGO_ANIMATE_TAILS
			screen.xoffset += 2

			object.timer++
			if object.timer == 2
				object.frame++
				if object.frame >= 16
					object.frame = 16
					object[+1].type 	= TypeName[Start Message]
					object[+1].priority = PRIORITY_ACTIVE
					object[+1].xpos 	= screen.xcenter
					object[+1].ypos 	= 216
					object.armPos.x = 8
					object.armPos.x += screen.xcenter
					object.armPos.y = -27
					object.armPos.y += screen.ycenter
					object.arrayPos = 0
					object.state++
				end if
				object.timer = 0
			end if

			if object.frame == 16
				DrawSpriteScreenXY(17, object.armPos.x, object.armPos.y)
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			break

		case LOGO_ARMAPPEAR_TAILS
			screen.xoffset += 2

			temp0 = object.arrayPos
			GetTableValue(object.armPos.x, temp0, Logo_tailsArmOffsets)
			object.armPos.x += screen.xcenter

			temp0++
			GetTableValue(object.armPos.y, temp0, Logo_tailsArmOffsets)
			object.armPos.y += screen.ycenter

			object.timer++
			object.timer &= 3
			if object.timer == 0
				object.arrayPos += 2
				if object.arrayPos == 4
					object.state++
				end if
			end if

			DrawSpriteScreenXY(17, object.armPos.x, object.armPos.y)
			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			break

		case LOGO_PREPLOGOAPPEAR_TAILS
			screen.xoffset += 2

			object.ypos++
			object.playerPos.y++
			object.armPos.y++
			if object.ypos >= 24
				object.state++
			end if

			DrawSpriteScreenXY(9, screen.xcenter, object.ypos)
			DrawSpriteScreenXY(17, object.armPos.x, object.armPos.y)
			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			break

		case LOGO_SHOWLOGO_TAILS
			screen.xoffset += 2

			if object.charTitlePos >= 89
				if object.speedPercent == 0
					object.timer++
					if object.timer == 3
						object.charTitlePos = 89
						object.state++
					end if
					object.speedPercent = 1
				end if

				object.yvel -= 0x4000
				if object.yvel > 0
					object.yvel -= 0x8000
				end if
			else
				object.speedPercent = 0
				object.yvel += 0x6000
				if object.yvel < 0
					object.yvel += 0x8000
				end if
			end if

			object.speedPercent += object.yvel
			temp0 = object.speedPercent
			temp0 >>= 16
			object.charTitlePos += temp0
			object.speedPercent &= 0xFFFF

			DrawSpriteScreenXY(17, object.armPos.x, object.armPos.y)
			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)

			if options.region == 1
				DrawSpriteScreenXY(33, screen.xcenter, object.charTitlePos)
				DrawSpriteScreenXY(34, screen.xcenter, object.charTitlePos)
			else
				DrawSpriteScreenXY(31, screen.xcenter, object.charTitlePos)
				DrawSpriteScreenXY(32, screen.xcenter, object.charTitlePos)
			end if

			DrawSpriteScreenXY(35, screen.xcenter, object.charTitlePos)
			break

		case LOGO_IDLE_TAILS
			screen.xoffset += 2

			DrawSpriteScreenXY(17, object.armPos.x, object.armPos.y)
			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)

			if options.region == 1
				DrawSpriteScreenXY(33, screen.xcenter, object.charTitlePos)
				DrawSpriteScreenXY(34, screen.xcenter, object.charTitlePos)
			else
				DrawSpriteScreenXY(31, screen.xcenter, object.charTitlePos)
				DrawSpriteScreenXY(32, screen.xcenter, object.charTitlePos)
			end if

			DrawSpriteScreenXY(35, screen.xcenter, object.charTitlePos)
			break
			
		end switch
		break

	case 2
		switch object.state
		case LOGO_SETUP_KNUX
			screen.xoffset = 0

			object.priority 	 = PRIORITY_ACTIVE
			object.timer 		 = 0x100
			stage.activeLayer[0] = 1
			object.state++

			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
			break

		case LOGO_FADEIN_KNUX
			screen.xoffset += 2

			if object.timer > 0
				object.timer -= 8
			else
				PlayMusic(0)
				object.state++
			end if

			SetScreenFade(0x00, 0x00, 0x00, object.timer)
			break

		case LOGO_RISEDELAY_KNUX
			screen.xoffset += 2

			object.timer++
			if object.timer == 38
				object.timer = 0
				object.playerPos.x = -44
				object.playerPos.x += screen.xcenter
				object.playerPos.y = -22
				object.playerPos.y += screen.ycenter
				object.frame = 18
				object.state++
			end if
			break

		case LOGO_RISE_KNUX
			screen.xoffset += 2

			object.playerPos.y -= 8
			temp0 = screen.ycenter
			temp0 -= 94
			if object.playerPos.y <= temp0
				object.state++
			end if

			temp0 += 32
			if object.playerPos.y == temp0
				object.frame++
				object.armFrame = 24
				object.armPos.x = -44
				object.armPos.x += screen.xcenter
				object.armPos.y = -46
				object.armPos.y += screen.ycenter
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			break

		case LOGO_ANIMATE_KNUX
			screen.xoffset += 2

			object.timer++
			if object.timer == 8
				object.frame++
				if object.frame >= 23
					object.frame = 23
					object[+1].type 	= TypeName[Start Message]
					object[+1].priority = PRIORITY_ACTIVE
					object[+1].xpos 	= screen.xcenter
					object[+1].ypos 	= 216
					object.state++
				end if

				object.timer = 0
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			if object.frame >= 23
				DrawSpriteScreenXY(object.armFrame, object.armPos.x, object.armPos.y)
			end if
			break

		case LOGO_ARMAPPEAR_KNUX
		case LOGO_ARMAPPEAR2_KNUX
			screen.xoffset += 2

			object.armPos.y--
			object.timer++
			if object.timer == 4
				object.timer = 0
				object.state++
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			DrawSpriteScreenXY(object.armFrame, object.armPos.x, object.armPos.y)
			break

		case LOGO_ANIMATEARM_KNUX
		case LOGO_ANIMATEARM2_KNUX
			screen.xoffset += 2

			object.timer++
			if object.timer == 4
				object.timer = 0
				object.armFrame++
				if object.armFrame == 29
					object.armFrame = 24
					object.state++
				end if
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			DrawSpriteScreenXY(object.armFrame, object.armPos.x, object.armPos.y)
			break

		case LOGO_MOVEARM_KNUX
		case LOGO_MOVEARM2_KNUX
			screen.xoffset += 2

			object.armPos.y++
			object.timer++
			if object.timer == 4
				object.timer = 0
				object.state++
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			DrawSpriteScreenXY(object.armFrame, object.armPos.x, object.armPos.y)
			break

		case LOGO_PREPLOGOAPPEAR_KNUX
			screen.xoffset += 2

			object.yvel += 0x4000
			object.speedPercent += object.yvel
			temp0 = object.speedPercent
			temp0 >>= 16
			object.ypos += temp0
			object.playerPos.y += temp0
			object.armPos.y += temp0
			object.speedPercent &= 0xFFFF

			if object.yvel > 0
				if object.ypos >= 24
					object.yvel >>= 1
					temp0 = object.yvel
					temp0 >>= 1
					object.yvel += temp0
					object.timer++
					if object.timer != 3
						FlipSign(object.yvel)
					else
						object.timer = 0
						object.yvel = 0
						object.state++
					end if
				end if
			end if

			DrawSpriteScreenXY(9, screen.xcenter, object.ypos) // Logo (Top Half)
			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			DrawSpriteScreenXY(object.armFrame, object.armPos.x, object.armPos.y)
			break

		case LOGO_SHOWLOGO_KNUX
			screen.xoffset += 2

			object.yvel += 0x4000
			object.speedPercent += object.yvel
			temp0 = object.speedPercent
			temp0 >>= 16
			object.charTitlePos += temp0
			object.speedPercent &= 0xFFFF

			if object.yvel > 0
				if object.charTitlePos >= 89
					object.yvel >>= 2
					temp0 = object.yvel
					temp0 >>= 1
					object.yvel += temp0
					object.timer++
					if object.timer != 2
						FlipSign(object.yvel)
					else
						object.timer = 0
						object.yvel = 0
						object.state++
					end if
				end if
			end if

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			DrawSpriteScreenXY(object.armFrame, object.armPos.x, object.armPos.y)
			DrawSpriteScreenXY(30, screen.xcenter, object.charTitlePos) // "Knux In" sprite
			break

		case LOGO_IDLE_KNUX
			screen.xoffset += 2

			DrawSpriteScreenXY(object.frame, object.playerPos.x, object.playerPos.y)
			DrawSpriteScreenXY(object.armFrame, object.armPos.x, object.armPos.y)
			DrawSpriteScreenXY(30, screen.xcenter, object.charTitlePos) // "Knux In" sprite
			break
			
		end switch
		break
		
	end switch

	// Logo (Bottom Half)
	DrawSpriteScreenXY(10, screen.xcenter, object.ypos)

	// Copyright Sprite
	temp0 = screen.xsize
	temp0 -= 96
	DrawSpriteScreenXY(11, temp0, 216)
end event


event ObjectStartup
	LoadSpriteSheet("Title/Title.gif")
	SetMusicTrack("Titlescreen.ogg", 0, false)

	SpriteFrame(-33, -74, 65, 64, 1, 1) 		// Sonic - Frame 1 		- #0
	SpriteFrame(-33, -74, 65, 88, 1, 1) 		// Sonic - Frame 2 		- #1
	SpriteFrame(-34, -66, 74, 88, 67, 1) 		// Sonic - Frame 3 		- #2
	SpriteFrame(-34, -66, 74, 88, 142, 1) 		// Sonic - Frame 4 		- #3
	SpriteFrame(-32, -72, 79, 88, 217, 1) 		// Sonic - Frame 5 		- #4
	SpriteFrame(-24, -74, 72, 88, 297, 1) 		// Sonic - Frame 6 		- #5
	SpriteFrame(-36, -76, 81, 88, 370, 1) 		// Sonic - Frame 7 		- #6
	SpriteFrame(-36, -76, 81, 88, 1, 90) 		// Sonic - Frame 8 		- #7
	SpriteFrame(-36, -76, 81, 88, 83, 90) 		// Sonic - Frame 9 		- #8
	SpriteFrame(-128, 42, 256, 74, 255, 90) 	// Logo (Top)			- #9
	SpriteFrame(-128, 112, 256, 74, 255, 167) 	// Logo (Bottom)		- #10
	SpriteFrame(0, 0, 71, 17, 182, 210) 		// Copyright Sprite		- #11
	SpriteFrame(24, 16, 56, 64, 276, 360) 		// Tails - Frame 1		- #12
	SpriteFrame(3, 21, 89, 57, 165, 90) 		// Tails - Frame 2		- #13
	SpriteFrame(8, 16, 88, 61, 165, 148) 		// Tails - Frame 3		- #14
	SpriteFrame(6, 17, 89, 59, 333, 360) 		// Tails - Frame 4		- #15
	SpriteFrame(6, 12, 87, 65, 423, 360) 		// Tails - Frame 5		- #16
	SpriteFrame(8, 5, 16, 19, 452, 5) 			// Tails' Arm			- #17
	SpriteFrame(3, 15, 101, 47, 2, 439) 		// Knux - Frame 1		- #18
	SpriteFrame(3, 15, 101, 73, 2, 439) 		// Knux - Frame 2		- #19
	SpriteFrame(0, 9, 95, 79, 104, 433) 		// Knux - Frame 3		- #20
	SpriteFrame(1, 6, 105, 82, 200, 430) 		// Knux - Frame 4		- #21
	SpriteFrame(1, 4, 102, 84, 306, 428) 		// Knux - Frame 5		- #22
	SpriteFrame(5, 2, 91, 86, 409, 426) 		// Knux - Frame 6		- #23
	SpriteFrame(8, 5, 42, 43, 469, 5) 			// Knux' Arm - Frame 1	- #24
	SpriteFrame(8, 3, 40, 53, 218, 310) 		// Knux' Arm - Frame 2	- #25
	SpriteFrame(8, 8, 40, 39, 471, 49) 			// Knux' Arm - Frame 3	- #26
	SpriteFrame(8, 8, 40, 39, 471, 49) 			// Knux' Arm - Frame 4	- #27
	SpriteFrame(8, 3, 40, 53, 218, 310) 		// Knux' Arm - Frame 5	- #28
	SpriteFrame(8, 5, 42, 43, 469, 5) 			// Knux' Arm - Frame 6	- #29
	SpriteFrame(-124, -66, 248, 65, 1, 364) 	// Knux In				- #30
	SpriteFrame(-77, -66, 153, 42, 1, 222) 		// "Tails"				- #31
	SpriteFrame(-77, -19, 93, 10, 155, 231) 	// "Miles Prower"		- #32
	SpriteFrame(-77, -66, 153, 42, 1, 179) 		// "Miles"				- #33
	SpriteFrame(-77, -19, 103, 10, 155, 242) 	// "Tails Prower"		- #34
	SpriteFrame(52, -19, 24, 18, 165, 411) 		// "In"					- #35
	SpriteFrame(-101, -66, 28, 42, 189, 321) 	// "&"					- #36

	// Setup Player Palette Colours
	temp0 = 208
	temp1 = 0
	while temp1 < 14
		GetTableValue(temp2, temp1, Logo_playerPalette)
		SetPaletteEntry(0, temp0, temp2)
		temp0++
		temp1++
	loop
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
end event


event RSDKLoad
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-128, 42, 256, 74, 255, 90) 	// Logo (Top)			- #0
	SpriteFrame(-128, 112, 256, 74, 255, 167) 	// Logo (Bottom)		- #1
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event